package com.sapitest.keywords.asserts;

import org.testng.Assert;

/**
 * 基本类型的断言方法，包括数值、字符串、布尔值、null和空等特殊类型情况
 * @date 2019-01-13
 * @author Liuxu
 * @version 1.0.0
 * @since jdk1.8
 */
public class BaseAssert extends Assert {
    private static final String ASSERT_FAILED_TEMPLATE = "Expected [%s] %s [%s], but found [%s] %s [%s].";

    public static void numEquals(int actual, int expected){
        assertEquals(actual, expected, null);
    }

    public static void numEquals(double actual, double expected){
        assertEquals(actual, expected, null);
    }

    public static void numEquals(float actual, float expected){
        assertEquals(actual, expected, null);
    }

    public static void numEquals(long actual, long expected){
        assertEquals(actual, expected, null);
    }

    public static void numEquals(short actual, short expected){
        assertEquals(actual, expected, null);
    }

    public static void numGreaterThan(int actual, int expected){
        if(actual <= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">", expected, actual, "<=", expected));
        }
    }

    public static void numGreaterThan(double actual, double expected){
        if(actual <= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">", expected, actual, "<=", expected));
        }
    }

    public static void numGreaterThan(float actual, float expected){
        if(actual <= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">", expected, actual, "<=", expected));
        }
    }

    public static void numGreaterThan(long actual, long expected){
        if(actual <= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">", expected, actual, "<=", expected));
        }
    }

    public static void numGreaterThan(short actual, short expected){
        if(actual <= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">", expected, actual, "<=", expected));
        }
    }

    public static void numLessThan(int actual, int expected){
        if(actual >= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<", expected, actual, ">=", expected));
        }
    }

    public static void numLessThan(double actual, double expected){
        if(actual >= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<", expected, actual, ">=", expected));
        }
    }

    public static void numLessThan(float actual, float expected){
        if(actual >= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<", expected, actual, ">=", expected));
        }
    }

    public static void numLessThan(long actual, long expected){
        if(actual >= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<", expected, actual, ">=", expected));
        }
    }

    public static void numLessThan(short actual, short expected){
        if(actual >= expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<", expected, actual, ">=", expected));
        }
    }

    public static void numGreaterThanOrEquals(int actual, int expected){
        if(actual < expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">=", expected, actual, "<", expected));
        }
    }

    public static void numGreaterThanOrEquals(double actual, double expected){
        if(actual < expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">=", expected, actual, "<", expected));
        }
    }

    public static void numGreaterThanOrEquals(float actual, float expected){
        if(actual < expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">=", expected, actual, "<", expected));
        }
    }

    public static void numGreaterThanOrEquals(long actual, long expected){
        if(actual < expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">=", expected, actual, "<", expected));
        }
    }

    public static void numGreaterThanOrEquals(short actual, short expected){
        if(actual < expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, ">=", expected, actual, "<", expected));
        }
    }

    public static void numLessThanOrEquals(int actual, int expected){
        if(actual > expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<=", expected, actual, ">", expected));
        }
    }

    public static void numLessThanOrEquals(double actual, double expected){
        if(actual > expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<=", expected, actual, ">", expected));
        }
    }

    public static void numLessThanOrEquals(float actual, float expected){
        if(actual > expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<=", expected, actual, ">", expected));
        }
    }

    public static void numLessThanOrEquals(long actual, long expected){
        if(actual > expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<=", expected, actual, ">", expected));
        }
    }

    public static void numLessThanOrEquals(short actual, short expected){
        if(actual > expected){
            fail(String.format(ASSERT_FAILED_TEMPLATE, actual, "<=", expected, actual, ">", expected));
        }
    }

    public static void strEquals(String actual, String expected){
        if(!actual.equals(expected)){
            fail(String.format("Expected [%s] equals [%s], but found they do not equal.", actual, expected));
        }
    }

    public static void strEqualsIgnoreCase(String actual, String expected){
        if(!actual.equalsIgnoreCase(expected)){
            fail(String.format("Expected [%s] equals [%s] ignore case, but found they do not equal ignore case.", actual, expected));
        }
    }

    public static void strNotEquals(String actual, String expected){
        if(actual.equals(expected)){
            fail(String.format("Expected [%s] does not equals [%s] ignore case, but found they equal.", actual, expected));
        }
    }

    public static void strNotEqualsIgnoreCase(String actual, String expected){
        if(actual.equalsIgnoreCase(expected)){
            fail(String.format("Expected [%s] does not equals [%s] ignore case, but found they equal ignore case.", actual, expected));
        }
    }

    public static void strContain(String actual, String expected){
        if(!actual.contains(expected)){
            fail(String.format("Expected [%s] contain [%s], but found [%s] does not contain [%s].", actual, expected, actual, expected));
        }
    }

    public static void strContainIgnoreCae(String actual, String expected){
        if(!actual.toLowerCase().contains(expected.toLowerCase())){
            fail(String.format("Expected [%s] contain [%s], but found [%s] does not contain [%s].", actual, expected, actual, expected));
        }
    }

    public static void strIn(String actual, String expected){
        if(!expected.contains(actual)){
            fail(String.format("Expected [%s] is in [%s], but found [%s] is not in [%s].", actual, expected, actual, expected));
        }
    }

    public static void strInIgnoreCase(String actual, String expected){
        if(!expected.toLowerCase().contains(actual.toLowerCase())){
            fail(String.format("Expected [%s] is in [%s] ignore case, but found [%s] is not in [%s] ignore case.", actual, expected, actual, expected));
        }
    }

    public static void strNotContain(String actual, String expected){
        if(actual.contains(expected)){
            fail(String.format("Expected [%s] does not contain [%s], but found [%s] contain [%s].", actual, expected, actual, expected));
        }
    }

    public static void strNotContainIgnoreCase(String actual, String expected){
        if(actual.toLowerCase().contains(expected.toLowerCase())){
            fail(String.format("Expected [%s] does not contain [%s] ignore case, but found [%s] contain [%s] ignore case.", actual, expected, actual, expected));
        }
    }

    public static void strStartWith(String actual, String expected){
        if(!actual.startsWith(expected)){
            fail(String.format("Expected [%s] start with [%s], but found [%s] does not start with [%s].", actual, expected, actual, expected));
        }
    }

    public static void strNotStartWith(String actual, String expected){
        if(actual.startsWith(expected)){
            fail(String.format("Expected [%s] does not start with [%s], but found it start with [%s].", actual, expected, expected));
        }
    }

    public static void strStartWith(String actual, String expected, int toffset){
        if(!actual.startsWith(expected, toffset)){
            fail(String.format("Expected [%s] start with [%s] at the specified index<%d>, but found [%s] does not start with [%s].", actual, expected, toffset, actual, expected));
        }
    }

    public static void strStartWithIgnoreCase(String actual, String expected, int toffset){
        if(!actual.toLowerCase().startsWith(expected.toLowerCase(), toffset)){
            fail(String.format("Expected [%s] start with [%s] ignore case at the specified index<%d>, but found [%s] does not start with [%s] ignore case.", actual, expected, toffset, actual, expected));
        }
    }

    public static void strStartWithIgnoreCase(String actual, String expected){
        if(!actual.toLowerCase().startsWith(expected.toLowerCase())){
            fail(String.format("Expected [%s] start with [%s] ignore case, but found [%s] does not start with [%s] ignore case.", actual, expected, actual, expected));
        }
    }

    public static void strEndWith(String actual, String expected){
        if(!actual.endsWith(expected)){
            fail(String.format("Expected [%s] ends with [%s], but found %s does not end with %s.", actual, expected, actual, expected));
        }
    }

    public static void strNotEndWith(String actual, String expected){
        if(actual.endsWith(expected)){
            fail(String.format("Expected [%s] does not end with [%s], but found it end with [%s].", actual, expected, expected));
        }
    }

    public static void strEndWithIgnoreCase(String actual, String expected){
        if(!actual.toLowerCase().endsWith(expected.toLowerCase())){
            fail(String.format("Expected [%s] end with [%s] ignore case, but found [%s] does not end with [%s] ignore case.", actual, expected, actual, expected));
        }
    }

    public static void strLengthIs(String actual, int expected){
        numEquals(actual.length(), expected);
    }

    public static void isTrue(boolean actual){
        assertTrue(actual);
    }

    public static void isFalse(boolean actual){
        assertFalse(actual);
    }

    public static void isEmpty(String actual){
        if(!actual.isEmpty()){
            fail(String.format("Expected [%s] is empty, but found it not empty.", actual));
        }
    }

    public static void isNotEmpty(String actual){
        if(actual.isEmpty()){
            fail(String.format("Expected [%s] is not empty, but found it empty.", actual));
        }
    }

    public static void isNUll(String actual){
        if (!(null == actual)){
            fail(String.format("Expected [%s] is null, but found it is not null.", actual));
        }
    }

    public static void isNotNUll(String actual){
        if (null == actual){
            fail(String.format("Expected [%s] is not null, but found it is null.", actual));
        }
    }
}
